using System;
using System.Windows.Controls;

namespace Insight.Library.Navigation
{
    /// <summary>
    /// these event arguments are provided from the AuthenticatingUriMapper
    /// in the AuthenticationRequired events to allow the listener to decide
    /// how to process a situation where authentication is required
    /// </summary>
    public class AuthenticationRequiredEventArgs : EventArgs
    {
        private readonly Type _typeofPage;
        private object[] _attributes;
        public AuthenticatingUriMapper Mapper { get; set; }

        /// <summary>
        /// the frame that will contain the page it will be navigated to
        /// </summary>
        public Frame Frame { get; set; }

        /// <summary>
        /// the URI being navigated to. This is the URI after before mapped,
        /// this will be navigated to after the login dialog has been displayed
        /// </summary>
        public Uri UnmappedUri { get; set; }
        /// <summary>
        /// the URI being navigated to. This is the URI after after mapped,
        /// </summary>
        public Uri MappedUri { get; set; }
        /// <summary>
        /// this is the attribute that is causing the event to be fired
        /// because authorization failed
        /// </summary>
        public AuthorizationAttribute AuthorizationAttribute { get; set; }

        /// <summary>
        /// if true, indicates navigation should be canceled after the event has 
        /// been processed.  if false the URI will be returned for navigation, thus 
        /// avoiding any authentication. you may wish to use this in situations where
        /// you want to override requirement forthe authentication in the event handler
        /// set to true by default
        /// </summary>
        public bool CancelNavigation { get; set; }

        public AuthenticationRequiredEventArgs(AuthenticatingUriMapper mapper, Type typeofPage, Frame frame, object [] attributes, Uri unmappedUri, Uri mappedUri, AuthorizationAttribute authorizationAttribute)
        {
            Mapper = mapper;
            Frame = frame;
            UnmappedUri = unmappedUri;
            MappedUri = mappedUri;
            AuthorizationAttribute = authorizationAttribute;
            _typeofPage = typeofPage;
            _attributes = attributes;
            CancelNavigation = true;
        }

        /// <summary>
        /// check the attributes of the current page to verify if the user is now authorized. This is used
        /// after the user has logged in via the login dialog, to verify that the new credentials meet the 
        /// level of authentication required
        /// </summary>
        /// <returns>null, if authorized. Otherwise the attribute that causes the authentication to fail</returns>
        public AuthorizationAttribute RecheckAttributes()
        {
            return Mapper.CheckAttributesForPage(_typeofPage, _attributes);
        }
    }
}